﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Battle
{
    public class PlunderBattleOver : IBattleOver
    {
        public BattleType GetBattleType()
        {
            return BattleType.PLUNDER;
        }

        bool localResult;
        bool remoteResult;

        OnBattleResult onResult;
        public PlunderBattleOver(OnBattleResult onResult)
        {
            this.onResult = onResult;
        }

        public void Excute()
        {
            localResult = BattleManager.Instance.battleViewCtrl.winCamp == BattleCamp.Attacker;
            ProtoBuf.Message.SCFightTreasureStartResult msg = StaticData.lastBattleEndMsg as ProtoBuf.Message.SCFightTreasureStartResult;
            remoteResult = msg.isWin == 1 ? true : false;
            StaticData.isLastBattleWin = remoteResult;
            End();
            //AskPlunderFightEnd();
        }

        void AskPlunderFightEnd()
        {
            ProtoBuf.Message.CSFightTreasureEnd msg = new ProtoBuf.Message.CSFightTreasureEnd();
            msg.fightPlayerId = StaticData.curBattleData.defendFighters[0].role.playerUID;
            msg.fragId = StaticData.curBattleData.targetFrag;
            msg.isWin = localResult ? 1 : 0;
            NetAPI.SendHttp(ProtoBuf.Message.OpDefine.CSFightTreasureEnd, msg, (pack, bSuccess) =>
            {
                if (bSuccess)
                {
                    ProtoBuf.Message.SCFightTreasureEnd _msg = pack.kBody as ProtoBuf.Message.SCFightTreasureEnd;
                    remoteResult = _msg.isServerWin == 1 ? true : false;
                    StaticData.lastBattleEndMsg = _msg;
                    StaticData.isLastBattleWin = remoteResult;
                    End();
                }
            });
        }

        public void End()
        {
            if (onResult != null)
            {
                onResult(remoteResult);
            }
        }
    }
}
